package com.cts.learninglibrary.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author arctique
 * @date 2020/8/24 9:31
 */
@Slf4j
public class CommonConverterUtil {

    /**
     * @Description: 对象转换工具
     * @Date: 2020/3/3 10:16
     * @param source: 资源
     * @param tClass: 结果类
     * @return: T
     **/
    public static <S,T>  T convert(S source,Class<T> tClass) {
        T target=null;
        try {
            target= tClass.newInstance();
            BeanUtils.copyProperties(source,target);
        } catch (InstantiationException e) {
            log.error("对象转换出错");
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            log.error("对象转换出错");
            e.printStackTrace();
        }
        return target;
    }

    public static <S,T> List<T> convert(List<S> sourceList, Class<T> tClass) {
        return sourceList.stream().map(e -> convert(e,tClass)
        ).collect(Collectors.toList());
    }
}
